package com.centaline.androidsalesblog.fragments.listmap;

import android.os.Bundle;

import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.model.LatLng;
import com.centaline.androidsalesblog.base.BaseListMapFragment;
import com.centaline.androidsalesblog.constants.ExtraConstant;
import com.centaline.androidsalesblog.iml.BaiduMarkerClick;
import com.centaline.androidsalesblog.iml.BottomSheetStatus;
import com.centaline.androidsalesblog.model.businissobject.PostMapBo;
import com.centaline.androidsalesblog.model.databaseobject.PostDo;
import com.centaline.androidsalesblog.overlay.local.PostMapOverlay;

import java.util.ArrayList;
import java.util.Locale;

/**
 * 租售房源列表-地图模式
 * Created by guilin on 16/4/12.
 */
public class PostListMapFragment extends BaseListMapFragment<PostDo> {

    private PostMapOverlay postMapOverlay;

    private ArrayList<PostMapBo> postMapBos = new ArrayList<>();

    protected BottomSheetStatus<PostMapBo> bottomSheetStatus;

    public void setBottomSheetStatus(BottomSheetStatus<PostMapBo> bottomSheetStatus) {
        this.bottomSheetStatus = bottomSheetStatus;
    }

    @Override
    protected void initView() {
        super.initView();
        postMapOverlay = new PostMapOverlay(getActivity(), baiduMap, postMapBos, new BaiduMarkerClick() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                if (marker != null) {
                    Bundle bundle = marker.getExtraInfo();
                    if (bundle != null) {
                        if (bundle.getParcelable(ExtraConstant.MAP_EXTRA_INFO) instanceof PostMapBo) {
                            PostMapBo postMapBo = bundle.getParcelable(ExtraConstant.MAP_EXTRA_INFO);
                            if (postMapBo != null) {
                                PostDo postDo = postMapBo.getPostDos().get(0);
                                if (overlay != null) {
                                    overlay.remove();
                                }
                                atv_mark_num.setText(String.format(Locale.CHINA, "%s套", postMapBo.getPostDos().size()));
                                atv_mark_title.setText(postMapBo.getCestName());
                                LatLng latLng = new LatLng(postDo.getLat(), postDo.getLng());
                                overlay = baiduMap.addOverlay(new MarkerOptions()
                                        .position(latLng)
                                        .anchor(0.2f, 1.0f)
                                        .icon(BitmapDescriptorFactory.fromView(clickView)));
                                baiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(latLng));
                                bottomSheetStatus.bottomSheetCallback(postMapBo, 0);
                                return true;
                            }
                        }
                    }
                }
                return false;
            }
        });
        baiduMap.setOnMarkerClickListener(postMapOverlay);
    }

    @Override
    protected void resetMap() {
        postMapBos.clear();
        calculateList();
        mapStatusChangeTag = 1;
        postMapOverlay.addFirstToMap();
    }

    @Override
    protected void refreshMap() {
        calculateList();
    }

    /**
     * 计算列表数据
     */
    private void calculateList() {
        for (PostDo postDo : mapArrayList) {
            String cestCode = postDo.getCestCode();
            if (keyArray.contains(cestCode)) {
                postMapBos.get(keyArray.indexOf(cestCode)).add(postDo);
            } else {
                keyArray.add(cestCode);
                PostMapBo postMapBo = new PostMapBo(cestCode, postDo.getCnEstate());
                postMapBo.add(postDo);
                postMapBos.add(postMapBo);
            }
        }
    }

    @Override
    public void onMapStatusChangeFinish(MapStatus mapStatus) {
        super.onMapStatusChangeFinish(mapStatus);
        if (mapStatusChangeTag == 1) {
            postMapOverlay.addToMap(false);
        } else {
            postMapOverlay.mapStatusChange();
        }
        mapStatusChangeTag = 0;
    }
}
